/*!
    \file    main.c
    \brief   EXTI Key Interrupt demo

    \version 2022-04-26, V2.0.0, demo for GD32F4xx
*/
#include "gd32f4xx.h"
#include "gd32f427v_start.h"
#include "systick.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "car.h"

void led_flash(uint8_t times);

int move_flag = 0;
int exec_flag = 0;
void move_test(car_type car)
{
		car_move(&car, car_forward);
		delay_1ms(1600);
		car_move(&car, car_stop);
		delay_1ms(1000);
		car_move(&car, car_right);
		delay_1ms(2000);
		car_move(&car, car_stop);
		delay_1ms(1000);
		car_move(&car, car_back);
		delay_1ms(1600);
		car_move(&car, car_stop);
		delay_1ms(1000);
		car_move(&car, car_left);
		delay_1ms(1850);
		car_move(&car, car_stop);
		delay_1ms(1000);
		
		
		car_move(&car, car_forward_right);
		delay_1ms(2400);
		car_move(&car, car_stop);
		delay_1ms(1000);
		car_move(&car, car_back_left);
		delay_1ms(2400);
		car_move(&car, car_stop);
		delay_1ms(1000);
	
		car_move(&car, car_right);
		delay_1ms(2000);
		car_move(&car, car_stop);
		delay_1ms(1000);
		
		car_move(&car, car_forward_left);
		delay_1ms(2400);
		car_move(&car, car_stop);
		delay_1ms(1000);
		car_move(&car, car_back_right);
		delay_1ms(2400);
		car_move(&car, car_stop);
		delay_1ms(1000);
		
		car_move(&car, car_left);
		delay_1ms(1000);
		car_move(&car, car_forward);
		delay_1ms(800);
		
		car_move(&car, car_stop);
		delay_1ms(1000);
		car_move(&car, car_turn_left);
		delay_1ms(2000);
		car_move(&car, car_stop);
		delay_1ms(1000);
		car_move(&car, car_turn_right);
		delay_1ms(2000);
		
		car_move(&car, car_stop);
}
int main(void)
{
		car_type car;
    /* configure systick */
    systick_config();

    /* enable the LEDs GPIO clock */
    rcu_periph_clock_enable(RCU_GPIOC);

    /* configure LED2 GPIO port */
    gpio_mode_set(GPIOC, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_6);
    gpio_output_options_set(GPIOC, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_6);
    /* reset LED2 GPIO pin */
    gpio_bit_reset(GPIOC, GPIO_PIN_6);
	
		memset(&car,0x00,sizeof(car));
		car_init(&car);
	
    /* flash the LED for test */
    led_flash(2);

    /* enable the key clock */
    rcu_periph_clock_enable(RCU_GPIOA);
    rcu_periph_clock_enable(RCU_SYSCFG);

    /* configure key pin as input */
    gpio_mode_set(GPIOA, GPIO_MODE_INPUT, GPIO_PUPD_NONE, GPIO_PIN_0);

    /* enable and set key EXTI interrupt to the lowest priority */
    nvic_irq_enable(EXTI0_IRQn, 2U, 0U);

    /* connect key EXTI line to key GPIO pin */
    syscfg_exti_line_config(EXTI_SOURCE_GPIOA, EXTI_SOURCE_PIN0);

    /* configure key EXTI line */
    exti_init(EXTI_0, EXTI_INTERRUPT, EXTI_TRIG_FALLING);
    exti_interrupt_flag_clear(EXTI_0);
		//move_test(car);
		
		delay_1ms(1000);
		car_move(&car, car_stop);
		delay_1ms(1000);
		/*
		car_move(&car, car_turn_left);
		delay_1ms(1000);
		car_move(&car, car_turn_right);
		delay_1ms(1000);
		*/
    while(1) {
			if(exec_flag)
			{	
				delay_1ms(1000);
				switch(move_flag){
					case 1:
						car_move(&car, car_forward);
						delay_1ms(1500);
						car_move(&car, car_stop);
						delay_1ms(1000);
						car_move(&car, car_back);
						delay_1ms(1500);
					break;
					case 2:
						car_move(&car, car_right);
						delay_1ms(2000);
						car_move(&car, car_stop);
						delay_1ms(1000);
						car_move(&car, car_left);
						delay_1ms(2000);
					break;
					case 3:
						car_move(&car, car_forward_right);
						delay_1ms(2400);
						car_move(&car, car_stop);
						delay_1ms(1000);
						car_move(&car, car_back_left);
						delay_1ms(2400);
					break;
					case 4:
						car_move(&car, car_forward_left);
						delay_1ms(2400);
						car_move(&car, car_stop);
						delay_1ms(1000);
						car_move(&car, car_back_right);
						delay_1ms(2400);
					break;
					case 5:
						car_move(&car, car_turn_left);
						delay_1ms(2000);
						car_move(&car, car_stop);
						delay_1ms(1000);
						car_move(&car, car_turn_right);
						delay_1ms(2000);
					break;
					default:
						move_test(car);
						car_move(&car, car_stop);
					break;
				}
				car_move(&car, car_stop);
				exec_flag = 0;
		  }
   }
}

/*!
    \brief      flash the LED for test
    \param[in]  times: times to flash the LEDs
    \param[out] none
    \retval     none
*/
void led_flash(uint8_t times)
{
    uint8_t i;
    for(i = 0; i < times; i++) {
        /* delay 500 ms */
        delay_1ms(500);
        /* turn on the LED */
        gpio_bit_set(GPIOC, GPIO_PIN_6);

        /* delay 500 ms */
        delay_1ms(500);
        /* turn off the LED */
        gpio_bit_reset(GPIOC, GPIO_PIN_6);
    }
}
